package com.market.controller;

import com.market.entity.IntoStorage;
import com.market.service.IntoStorageService;
import com.market.vo.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/IntoStorage")
public class IntoStorageController {

    @Resource
    public IntoStorageService intoStorageService;

    //新增入库记录
    @PostMapping("/addIntoStorageRecord")
    public R<IntoStorage> addIntoStorageRecord(@RequestBody IntoStorage intoStorage) {
        boolean b = intoStorageService.addIntoStorageRecord(intoStorage);
        if (b) {
            return new R<>(200, "新增入库记录成功",intoStorage);
        } else {
            return new R<>(300, "新增入库记录成功");
        }
    }

    //修改实际入库数量
    @GetMapping("/updateRealIntoStorageNum")
    public R<IntoStorage> updateRealIntoStorageNum(int id,int updateRealIntoStorageNum) {
        boolean b = intoStorageService.updateRealIntoStorageNum(id, updateRealIntoStorageNum);
        if (b) {
            return new R<>(200, "修改实际入库数量成功");
        } else {
            return new R<>(300, "修改实际入库数量成功");
        }
    }

    //通过id修改一条入库记录
    @PostMapping("/updateIntoStorageRecord")
    public R<IntoStorage> updateIntoStorageRecord(@RequestBody IntoStorage intoStorage) {
        boolean b = intoStorageService.updateIntoStorageRecord(intoStorage);
        if (b) {
            return new R<>(200, "修改入库记录成功");
        } else {
            return new R<>(300, "修改入库记录失败");
        }
    }

    //通过id查询一条入库记录
    @GetMapping("/selectARecord")
    public R<IntoStorage> selectARecord(int id) {
        IntoStorage intoStorage = intoStorageService.selectARecord(id);
        if (intoStorage != null) {
            return new R<>(200, "查询入库记录成功", intoStorage);
        } else {
            return new R<>(300, "查询入库记录失败");
        }
    }

    //查询时间为某一天的所有入库记录

    @GetMapping("/selectAllRecordsInADay")
    public R<List<IntoStorage>> selectAllRecordsInADay(String day) {
        List<IntoStorageService> intoStoragesInADay = intoStorageService.selectAllRecordsInADay(day);
        if (intoStoragesInADay != null) {
            return new R(200, "查询入库记录成功", intoStoragesInADay);
        } else {
            return new R(300, "查询入库记录失败");
        }
    }

    //根据id删除一条入库记录
    @GetMapping("/deleteARecord")
    public R<IntoStorage>deleteARecord(int id){
        boolean b = intoStorageService.deleteARecord(id);
        if (b) {
            return new R<>(200, "删除入库记录成功");
        } else {
            return new R<>(300, "删除入库记录失败");
        }
    }
}
